package org.openCage.artig;

import org.kohsuke.args4j.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Elephants {


    public static void main(String[] args) {
        new Elephants().parse( args ).go();
    }

    public static class NullCmd implements CmdArgs {

        @Override public String getName() {
            return "NULLLLL";
        }

        @Override
        public void go() {
        }

        @Override
        public String usage() {
            return "nix gar nix";
        }
    }

    @Argument()
    private List<String> arguments = new ArrayList<String>();

    // general options
    @Option( name="-v", aliases = {"--verbose"}, usage = "turn on verbose messages")
    private boolean verbose;

    @Option( name="-p", aliases = {"--project"}, metaVar = "DIR", usage = "the project root, (defaults to current dir)")
    private String dir;

    // per command options                                                                                         
    @Option( name = "-t", aliases = {"--type"}, metaVar = "ArtigType", usage = "(create) type of artig: project, module...")    
    private String typ;

    @Option( name = "-g", aliases = {"--group"}, metaVar="groupId", usage = "(create) groupid (think maven)" )
    private String group;

    @Option( name="-m", aliases = {"--module"}, metaVar = "MODULE", usage = "the module to build" )
    private String module;





    private Map<String,CmdArgs> cmds = new HashMap<String, CmdArgs>();

    public Elephants() {
        cmd( new GoCreate() );
        cmd( new GoAnt());
        cmd( new GoMavenSetup());
        cmd( new GoCreateDeployed());
        cmd( new GoTest());
        cmd( new GoCheckUpdates());
        cmd( new GoBuild());
        cmd( new GoRebuild());
        cmd( new GoMarch());
    }

    private Elephants cmd( CmdArgs cargs ) {
        cmds.put( cargs.getName(), cargs );
        return this;
    }


    public CmdArgs parse( String ... args ) {
        CmdLineParser baseParser = new CmdLineParser(this);
        try {
            baseParser.parseArgument( args );
        } catch (CmdLineException e) {
            System.out.println( e.getMessage() );
            return usage( baseParser );
        }

        if ( arguments.size() == 0 ) {
            System.out.println("choose one of the commands");
            return usage( baseParser );
        }

        String cmdWord = arguments.get(0);

        if ( cmdWord.equals( "help") ) {
            return usage( baseParser );
        }

        if ( !cmds.containsKey( cmdWord ) ) {
            System.out.println( "not a supported command: " + cmdWord );
            return usage( baseParser );
        }


        CmdArgs details = cmds.get( arguments.get(0));
        CmdLineParser detailsParser = new CmdLineParser(details );
        try {
            detailsParser.parseArgument( args );
        } catch (CmdLineException e) {
            System.out.println( e.getMessage() );
            return usage( baseParser );
        }

        return details;
    }

    private NullCmd usage( CmdLineParser baseParser ) {
        System.out.println("Elephants");
        //baseParser.printUsage( System.out );
        //System.out.println ("commands: ");

        for ( CmdArgs cmd : cmds.values() ) {
            System.out.println( cmd.getName() + " " + cmd.usage() );
            CmdLineParser detailsParser = new CmdLineParser( cmd );
            //System.out.println( detailsParser.printExample(ExampleMode.ALL ));
            detailsParser.printUsage( System.out );
        }
        return new NullCmd();
    }
}
